#include "Subsystems/BlockBehaviors/SubsystemSnowBlockBehavior.h"
#include "Subsystems/SubsystemTerrain.h"
#include "GameSingleton.h"
#include "Managers/BlocksManager.h"

#include "Blocks/SnowBlock.h"
#include "Blocks/OakLeavesBlock.h"
#include "Blocks/BirchLeavesBlock.h"
#include "Blocks/MimosaLeavesBlock.h"
#include "Blocks/SpruceLeavesBlock.h"
#include "Blocks/TallSpruceLeavesBlock.h"

namespace PocketSurvival
{

    SubsystemSnowBlockBehavior::SubsystemSnowBlockBehavior()
    {
        m_handleBlocks.push_back(SnowBlock::Index());
    }

    bool SubsystemSnowBlockBehavior::canSupportSnow(int32_t value)
    {
        int32_t blockContents = Terrain::ExtractContents(value);
        Block *block = GameSingleton::blocksManager->blocks[blockContents];
        if (block->IsTransparent)
        {
            switch (blockContents)
            {
            case OakLeavesBlock::Index():
            case BirchLeavesBlock::Index():
            case MimosaLeavesBlock::Index():
            case SpruceLeavesBlock::Index():
            case TallSpruceLeavesBlock::Index():
                return true;
            default:
                return false;
            }
        }
        return true;
    }

    void SubsystemSnowBlockBehavior::onNeighborBlockChanged(int32_t x, int32_t y, int32_t z, int32_t neighborX, int32_t neighborY, int32_t neighborZ)
    {
        if (false == canSupportSnow(m_terrain->getCellValue(x, y - 1, z)))
        {
            m_terrain->destroyCell(0, x, y, z, 0, false);
        }
    }

    void SubsystemSnowBlockBehavior::load(const nlohmann::json &json)
    {
        m_terrain = GameSingleton::singletonPtr->getSingleton<SubsystemTerrain>();
    }

    static const std::string subsystemName = "SnowBlockBehavior";
    const std::string &SubsystemSnowBlockBehavior::getName() const
    {
        return subsystemName;
    }

}